/**
 * 
 */
package HSFInterTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Assert;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import Utils.TimeUtils;

import com.de.deguic.domain.DegUsersDO;
import com.de.deguic.domain.DegUsersExtraDO;
import com.de.deguic.domain.ResultDTO;
import com.de.deguic.domain.enums.UserActiveEnum;
import com.de.deguic.domain.enums.UserJoinChannelEnum;
import com.de.deguic.domain.enums.UserJoinFromEnum;
import com.de.deguic.domain.enums.UserTypeEnum;

import dao.GenericDAO;

/**
 * @author qiyuan.zqy
 *
 */
public class CommonOps {
	
	public static GenericDAO deguicDao;
	public static GenericDAO bdmetaDao;
	
	static{
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
				"dao-beans.xml");
		deguicDao = (GenericDAO) context.getBean("deguicDao");
		bdmetaDao = (GenericDAO) context.getBean("bdmetaDao");
	}
	
	public static void verifyDegUicData(DegUsersDO userDO, long degUserId){
		if (userDO == null)
			Assert.fail("verifyDegUicData failed. returned userDO is null." +
					"userId is "+ degUserId);
		
		String tableName = "deg_users_main";
		Map<String,Object> conditions = new HashMap<String,Object>();
		conditions.put("user_id", degUserId);
		conditions.put("user_from", 10);
		List<Map> dbRecordList = CommonOps.deguicDao.query(tableName, conditions);		
		Assert.assertEquals(1, dbRecordList.size());
		
		Map dbRecord = dbRecordList.get(0);
		
		Assert.assertEquals((long)userDO.getUserId(), Long.parseLong(dbRecord.get("user_id").toString()));
		Assert.assertEquals(userDO.getBdUserStrId(), dbRecord.get("user_str_id").toString().split("#")[0]);
		Assert.assertEquals(userDO.getUserActive().numberValue(),Integer.parseInt(dbRecord.get("user_active").toString()));
		Assert.assertEquals(userDO.getUserFrom().numberValue(), Integer.parseInt(dbRecord.get("user_from").toString()));
		Assert.assertEquals(userDO.getUserType().numberValue(), Integer.parseInt(dbRecord.get("user_type").toString()));
		Assert.assertEquals((long)userDO.getRefUserId(), Long.parseLong(dbRecord.get("ref_user_id").toString()));
		if (userDO.getRefUserNick() != null )
			Assert.assertEquals(userDO.getRefUserNick(), dbRecord.get("ref_user_nick").toString());
		if (userDO.getPayAccountId() != null )
			Assert.assertEquals(userDO.getPayAccountId(), dbRecord.get("pay_account_id").toString());
		if (userDO.getPayAccountEmail() != null )
			Assert.assertEquals(userDO.getPayAccountEmail(), dbRecord.get("pay_account_email").toString());
		if (userDO.getLatestLoginIp()!=null)
			Assert.assertEquals(userDO.getLatestLoginIp(),dbRecord.get("latest_login_ip").toString());
		if (userDO.getGmtLatestLogin()!=null){
			String dbLastLoginDate = dbRecord.get("gmt_latest_login").toString();
			Assert.assertTrue(userDO.getGmtLatestLogin().equals(
					TimeUtils.getDateFromStr(dbLastLoginDate.substring(0, dbLastLoginDate.length()-2))));
		}
		if (userDO.getUserChannel() != null)
			Assert.assertEquals(userDO.getUserChannel().numberValue(), Integer.parseInt(dbRecord.get("user_channel").toString()));
//		if (userDO.getAttributes()!=null)
//			Assert.assertEquals(userDO.getAttributes(),dbRecord.get("attributes").toString());	
	}
	
	public static void verifyBdmetaData(DegUsersDO userDO, long userId){
		if (userDO == null)
			Assert.fail("verifyBdmetaData failed. returned userDO is null." +
					"userId is "+ userId);
		
		String tableName = "bd_user";
		Map<String,Object> conditions = new HashMap<String,Object>();
		conditions.put("id", userId);
		List<Map> dbRecordList = CommonOps.bdmetaDao.query(tableName, conditions);		
		Assert.assertEquals(1, dbRecordList.size());
		
		Map dbRecord = dbRecordList.get(0);
		Assert.assertEquals((long)userDO.getUserId(), Long.parseLong(dbRecord.get("id").toString()));
		Assert.assertEquals(dbRecord.get("nick").toString(),userDO.getRefUserNick());
		Assert.assertEquals(dbRecord.get("user_str_id").toString(),userDO.getBdUserStrId());
		Assert.assertNull(userDO.getUserFrom());
		Assert.assertNull(userDO.getUserChannel());
		
	}
	
	public static void verifyDegUicExtraData(DegUsersExtraDO userExtraDO, long degUserId){
		if (userExtraDO ==null)
			Assert.fail("verifyDegUicExtraData failed. returned userExtraDO is null." +
					"userId is "+ degUserId);
		
		String tableName = "deg_users_extra";
		Map<String,Object> conditions = new HashMap<String,Object>();
		conditions.put("user_id", degUserId);
		List<Map> dbRecordList = CommonOps.deguicDao.query(tableName, conditions);		
		Assert.assertEquals(1, dbRecordList.size());
		
		Map dbRecord = dbRecordList.get(0);
		
		Assert.assertEquals(Long.parseLong(dbRecord.get("user_id").toString()), (long)userExtraDO.getUserId());
		Assert.assertEquals(Long.parseLong(dbRecord.get("version").toString()), (long)userExtraDO.getVersion());
		Assert.assertEquals(Long.parseLong(dbRecord.get("user_tag1").toString()),userExtraDO.getUserTag1().getValue());
		Assert.assertEquals(Long.parseLong(dbRecord.get("user_tag2").toString()),userExtraDO.getUserTag2().getValue());
		Assert.assertEquals(Long.parseLong(dbRecord.get("user_tag3").toString()),userExtraDO.getUserTag3().getValue());
		Assert.assertEquals(Long.parseLong(dbRecord.get("user_tag4").toString()),userExtraDO.getUserTag4().getValue());
		Assert.assertEquals(Long.parseLong(dbRecord.get("user_tag5").toString()),userExtraDO.getUserTag5().getValue());
		Assert.assertEquals(Long.parseLong(dbRecord.get("user_tag6").toString()),userExtraDO.getUserTag6().getValue());
		Assert.assertEquals(Long.parseLong(dbRecord.get("user_tag7").toString()),userExtraDO.getUserTag7().getValue());
		Assert.assertEquals(Long.parseLong(dbRecord.get("user_tag8").toString()),userExtraDO.getUserTag8().getValue());
		Assert.assertEquals(Long.parseLong(dbRecord.get("user_tag9").toString()),userExtraDO.getUserTag9().getValue());
	}
	
	public static void recoverUserInfo(long degUserId){
		DegUsersDO inputUserDO = new DegUsersDO();
		
		inputUserDO.setUserId(degUserId);
		
		inputUserDO.setUserFrom(UserJoinFromEnum.TaoBao);
		inputUserDO.setUserChannel(UserJoinChannelEnum.ALI_GAME_SDK);
		inputUserDO.setUserType(UserTypeEnum.PLAYER);
		inputUserDO.setUserActive(UserActiveEnum.NORMAL);	
		inputUserDO.setLatestLoginIp("10.65.3.6");
		inputUserDO.setPassword("password1");
		inputUserDO.setPhone("12345678900");
		inputUserDO.setUserNick("recoverNick");
		inputUserDO.setGmtLatestLogin(TimeUtils.getDateFromStr("2014-07-18 11:12:13"));

		ResultDTO<DegUsersDO> result = BeanRepo.testDegUicWriteService
				.updateDegMainUserById(inputUserDO, "testApp");
		
		Assert.assertTrue(result.isSuccess());
	}
	
	public static boolean getTagValue(long degUserId, int tagNum, int bitNum){
		
		ResultDTO<DegUsersExtraDO> result = BeanRepo.testDegUicExtraReadService
				.getDegUserExtraByDegUserId(degUserId);
		
		if (!result.isSuccess()) {
			Assert.fail("getTagValue Failed. HSF interface invoke failed. "
					+ "Error msg is" + result.getMessage() + result.getError());
		}
		
		DegUsersExtraDO extraInfo = result.getModule();
		
		return extraInfo.getUserTag(tagNum).getBit(bitNum);
		
	}

}
